/*
 * Flashlib - QB9 common Flash library
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.qb9.flashlib.display.spritesheet
{
	import com.qb9.flashlib.geom.Anchor;
	import com.qb9.flashlib.prototyping.MockMovieClip;
	
	import flash.display.Bitmap;
	import flash.geom.Point;

	public class SpritedMovieClip extends MockMovieClip
	{
		private var source:SpriteSource;
		private var bmp:Bitmap;
		private var anchor:Anchor;
		
		// position can be an Anchor (relative) or a Point (fixed)
		public function SpritedMovieClip(source:SpriteSource, position:Object=null, labels:Array=null)
		{
			this.source = source;
			this.anchor = position as Anchor;
			
			super(source.length, labels);
			
			bmp = createBitmap();
			if (position is Point)
			{
				bmp.x = position.x;
				bmp.y = position.y;	
			}			
			addChild(bmp);
		}
		
		override public function set cacheAsBitmap(value:Boolean):void
		{
			// Don't cache
		}
		
		protected function createBitmap():Bitmap
		{
			return new Bitmap();
		}
		
		override protected function update():void
		{
			bmp.bitmapData = source.getSprite(currentFrame-1);
			
			if (!anchor)
				return;
			
			bmp.x = bmp.width * anchor.u;
			bmp.y = bmp.height * anchor.v;
		}
		
		override public function dispose():void
		{
			source = null;
			bmp = null;
			anchor = null;
			super.dispose();
		}
		
	}
}